package com.digital.smartgrow.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.common.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 公共接口 前端控制器
 * </p>
 *
 * @author xiewb
 * @since 2022-04-23
 */
@Api(tags = "公共接口")
@RestController
@RequestMapping("${context.prefix}/public")
public class PublicController {

    @ApiOperation(value = "上传文件")
    @PostMapping("/uploadFile")
    public Object uploadFile(MultipartFile file) throws IOException {
        if (file == null) return Result.fail("请选择文件");
        String fileName = FileUtil.saveFile(file);
        if (StringUtils.isNotBlank(fileName)) return Result.success(fileName);
        return Result.fail("文件上传失败");
    }
}
